package controller;

import aop.LogData;
import aop.LogType;
import com.github.pagehelper.PageInfo;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import service.SService;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/service")
public class ServiceController {
    @Autowired
    private SService sService;

    @ResponseBody
    @PostMapping("/list")
    public MyResult<List<entity.Service>> list(LayRequest layRequest){
        PageInfo<Service> servicePageInfo = sService.selectByPage(layRequest);
        return new MyResult(0, servicePageInfo.getTotal(),servicePageInfo.getList());
    }

    @RequestMapping("/toSelect")
    public String toSelect(ModelMap modelMap){
        List<Service> service = sService.selectWithCustomerName();
        modelMap.addAttribute("service",service);
        return "select.jsp";
    }

    @LogData(logType = LogType.INSERT,description = "添加服务")
    @ResponseBody
    @PostMapping("/add")
    public MyResult add(Service service, HttpSession session){
        Users user = (Users) session.getAttribute("admin");
        service.setCreater(user.getId());
        boolean flag=sService.insert(service);
        return new MyResult(0);
    }

    @RequestMapping("/toAdd")
    public String toAdd(ModelMap modelMap){
        List<Service> service=sService.selectWithCustomerName();
        modelMap.addAttribute("service",service);
        return "add.jsp";
    }
    @LogData(logType = LogType.UPDATE,description = "修改服务")
    @ResponseBody
    @PostMapping("/update")
    public MyResult update(Service service){
        boolean flag=sService.update(service);
        return new MyResult(0);
    }

    @RequestMapping("/toUpdate")
    public String toUpadte(Integer id,ModelMap modelMap){
        Service serviceSelf =sService.selectById(id);
        modelMap.addAttribute("serviceSelf",serviceSelf);
        List<Service> service = sService.selectWithCustomer();
        modelMap.addAttribute("service",service);
        return "update.jsp";
    }
    @LogData(logType = LogType.DELETE,description = "批量删除服务")
    @ResponseBody
    @PostMapping("/handleService")
    public MyResult handleService(Service service){
        boolean flag=sService.handleService(service);
        return new MyResult(0);
    }

    @RequestMapping("/toHandleService")
    public String toHandleService(Integer id,ModelMap modelMap){
        Service serviceSelf =sService.selectByServiceId(id);
        modelMap.addAttribute("serviceSelf",serviceSelf);
        List<Service> service = sService.selectWithCustomer();
        modelMap.addAttribute("service",service);
        return "handleService.jsp";
    }

    @ResponseBody
    @PostMapping("/batchDelete")
    public MyResult batchDelete(Integer[] ids){
        boolean flag=sService.batchDelete(ids);
        return new MyResult(0);
    }

    @RequestMapping("/toDeliver")
    public String toDeliver(ModelMap modelMap,Integer id){
        List<Service> service=sService.selectUserAccount();
        modelMap.addAttribute("service",service);
        Service serviceSelf =sService.selectById(id);
        modelMap.addAttribute("serviceSelf",serviceSelf);
        modelMap.addAttribute("service_id",id);
        return "deliver.jsp";
    }
    @LogData(logType = LogType.INSERT,description = "转交服务")
    @ResponseBody
    @PostMapping("/insertWithHandler")
    public MyResult insertWithHandler(Service_Transfer service_transfer,Integer new_manager_id,Integer service_id){
        boolean flag=sService.insertWithHandler(service_transfer);
        boolean flag1=sService.updateByNewanagerId(new_manager_id,service_id);
        return new MyResult(0);
    }

    @ResponseBody
    @PostMapping("/uploadImg")
    public MyResult<Map> uploadImg(MultipartFile file, HttpServletRequest request) throws IOException {
        String filename = file.getOriginalFilename();
        System.out.println("上传的文件名："+filename);
        File f = new File("D:/Test/serviceImg/" + filename);
        if (!f.getParentFile().exists()){
            f.getParentFile().mkdirs();//如果父目录不存在，创建该目录
        }
        //保存文件，将上传的文件内容写入file
        file.transferTo(f);
        Map map = new HashMap();
        map.put("src",request.getContextPath()+"/serviceImg/"+filename);//图片访问路径
        map.put("title",filename);//图片标题
        return new MyResult<>(0,"",map);
    }

    @RequestMapping("/toDetail")
    public String toDetail(ModelMap modelMap,Integer id) {
        Service serviceSelf =sService.selectById(id);
        modelMap.addAttribute("serviceSelf",serviceSelf);
//        List<Service_Transfer> service_transfer=sService.selectServiceTransfer(serviceSelf.getId());
        System.out.println(serviceSelf);
        Service serviceHandler =sService.selectByHandler(serviceSelf.getHandler());
        modelMap.addAttribute("serviceHandler",serviceHandler);
        Service serviceCustomer_id =sService.selectCustomer_id(serviceSelf.getCustomer_id());
        modelMap.addAttribute("serviceCustomer_id",serviceCustomer_id);
        return "detail.jsp";
    }

    @ResponseBody
    @PostMapping("/findServiceTransfersByServiceId")
    public MyResult findServiceTransfersByServiceId(Integer id){
        List<Service_Transfer> service_transfer=sService.selectServiceTransfer(id);
        return new MyResult(0,service_transfer);
    }

    @ResponseBody
    @PostMapping("/findServiceById")
    public MyResult findServiceById(Integer id){
        Service serviceSelf =sService.selectById(id);
        return new MyResult(0);
    }

    @RequestMapping("/toCustomerDetail")
    public String toCustomerDetail(ModelMap modelMap,Integer id){
        Service service = sService.selectByServiceIdWithCustomerId(id);
        Customer customer =sService.selectByCustomerId(service.getCustomer_id());
        modelMap.addAttribute("customer",customer);
        return "customerInfo.jsp";
    }

    @RequestMapping("/toUserDetail")
    public String toUserDetail(ModelMap modelMap,Integer id){
        Users user =sService.selectByUserId(id);
        modelMap.addAttribute("user",user);
        return "userInfo.jsp";
    }

    @ResponseBody
    @PostMapping("/customerCareList")
    public MyResult<List<CustomerCare>> customerCareList(LayRequest layRequest){
        PageInfo<CustomerCare> customerCarePageInfo = sService.customerCareList(layRequest);
        return new MyResult(0, customerCarePageInfo.getTotal(),customerCarePageInfo.getList());
    }
}
